﻿using HlELock.Core;
using HlELock.Domain;
using HlELock.Modules.Hotel.Main;
using HlELock.Page.Views;
using Microsoft.EntityFrameworkCore;
using Prism.Ioc;
using Prism.Modularity;
using Syncfusion.Licensing;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Windows;

namespace HlELock.Hotel
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App
    {
        public App()
        {
            SyncfusionLicenseProvider.RegisterLicense("MzI0NzEzNkAzMjM1MmUzMDJlMzBESjFZZUlkbzBPZnhORzFERkZSbm9WaENMQVhYN1JXaUowY0tkOTBZNFpJPQ==");
            MyAppContext.Init();

        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // 捕获 UI 线程异常
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
        }

        private void App_DispatcherUnhandledException(object sender,
            System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            // 记录异常
            System.Diagnostics.Debug.WriteLine($"UI 线程异常: {e.Exception.Message}");

            // 显示友好的错误消息
            MessageBox.Show(e.Exception.Message,
                           "错误", MessageBoxButton.OK, MessageBoxImage.Error);

            // 标记异常已处理，防止应用程序崩溃
            e.Handled = true;

            // 或者选择关闭应用程序
            // Application.Current.Shutdown();
        }
        protected override Window CreateShell()
        {
            using (var context = new HlELockDbContext())
            {
                context.Database.Migrate();
            }
            List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                dictionaryList.Add(dictionary);
            }
            string requestedCulture = @"Language\StringResource.zh-cn.xaml";
            ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture));
            if (resourceDictionary == null)
            {
                requestedCulture = @"Language\StringResource.zh-cn.xaml";
                resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture));
            }
            if (resourceDictionary != null)
            {
                Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }

            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<TestPage>();
            containerRegistry.RegisterForNavigation<LoginPage>();
            containerRegistry.RegisterForNavigation<SearchDeviceControl>();
            containerRegistry.RegisterForNavigation<SearchHotelDeviceControl>();
        }
        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<HotelMainModule>();
            moduleCatalog.AddModule<CoreModule>();
        }
    }
}
